package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbMember;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class fg_oneServlet
 */
public class fg_oneServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public fg_oneServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        BizDaoImpl bdi = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bdi);// 注入

        // 查看有没有此用户名
        String name = request.getParameter("name");
        PrintWriter out = response.getWriter();
        if (name.equals("")) {
            out.print("<script>");
            out.print("alert('表单信息不能为空!');");
            out.print("window.location.href='fg-One.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }

        TbMember tbmember = new TbMember();
        tbmember.setName(name);

        TbMember mem = bsi.getonttbmember(tbmember); // hui获取指定用户的全部信息

        boolean flag = bsi.getonetbmember(tbmember);
        HttpSession session = request.getSession();

        if (flag) {
            request.setAttribute("fgone", null);
            session.setAttribute("men", mem);
            request.getRequestDispatcher("fg-Two.jsp").forward(request, response);
        } else {
            request.setAttribute("fgone", 1);
            request.getRequestDispatcher("fg-Two.jsp").forward(request, response);
        }

    }
}
